package com.graduation.springbootinit.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.graduation.springbootinit.model.entity.ActivityPart.QuizRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface QuizRecordMapper extends BaseMapper<QuizRecord> {
    
    @Select("SELECT * FROM community_activities_quiz_record WHERE challenge_id = #{challengeId} ORDER BY total_score DESC LIMIT 3")
    List<QuizRecord> selectTopThree(Long challengeId);
    
    @Update("UPDATE community_activities_quiz_record SET ranking = #{ranking}, points_received = #{points} WHERE id = #{id}")
    void updateRankAndPoints(Long id, Integer ranking, Integer points);

    @Select("SELECT qr.*, qc.title AS challengeTitle " +
            "FROM community_activities_quiz_record qr " +
            "JOIN community_activities_quiz_challenge qc ON qr.challenge_id = qc.id " +
            "WHERE qr.user_id = #{userId} " +
            "ORDER BY qr.created_at DESC")
    List<QuizRecord> selectUserQuizHistory(Long userId);
} 